package Jd;

import org.dom4j.Attribute;
import org.dom4j.io.SAXReader;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

public class jd01 {
    public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException, TransformerException {
       ShowInfoJd04();
    }
    public static void ShowInfo1() throws ParserConfigurationException, IOException, SAXException {
        //得到DOM解析器的工厂实例
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        //从DOM工厂获得DOM解析器
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse("src/main/java/Jd/jd01.xml");

        //得到所有student节点信息
        NodeList studentList = doc.getElementsByTagName("student");
        NodeList names = doc.getElementsByTagName("name");
        NodeList courseList = doc.getElementsByTagName("course");
        NodeList scoreList = doc.getElementsByTagName("score");
        //循环id信息
        for (int i = 0; i < studentList.getLength(); i++) {
            //获取第i个student的信息
            Node student = studentList.item(i);
            //获取第i个student元素的name属性的值
            Element stuElement = (Element) student;
            String studentValue = stuElement.getAttribute("id");

            Element nameElement = (Element) names.item(i);
            String nameValue = nameElement.getAttribute("name");

            Element courseElement = (Element) courseList.item(i);
            String courseValue = courseElement.getAttribute("name");

            Element scoreElement = (Element) scoreList.item(i);
            String scoreValue = scoreElement.getAttribute("name");
            System.out.println("学生编号：" + studentValue + "\t姓名：" + nameValue + "\t学科：" + courseValue + "\t成绩：" + scoreValue);

        }
    }
    public static void ShowInfo2() throws ParserConfigurationException, IOException, SAXException, TransformerException {
        //得到DOM解析器的工厂实例
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        //从DOM工厂获得DOM解析器
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse("src/main/java/Jd/jd01.xml");
        //得到所有student节点信息
        NodeList studentList = doc.getElementsByTagName("student");
        for (int i=0;i<studentList.getLength();i++){
            Element stuElement=(Element) studentList.item(i);
            String idName=stuElement.getAttribute("id");
            if (idName.equals("1")){
                stuElement.getParentNode().removeChild(stuElement);
            }
        }
        //保存XML文件
        TransformerFactory transformerFactory=TransformerFactory.newInstance();
        Transformer transformer=transformerFactory.newTransformer();
        DOMSource domSource=new DOMSource(doc);
        //设置编码类型
        transformer.setOutputProperty(OutputKeys.ENCODING,"gb2312");
        StreamResult result=new StreamResult(new FileOutputStream("src/main/java/Jd/jd01.xml"));
        //把DOM树转换为XML文件
        transformer.transform(domSource,result);
    }
    public static void ShowInfo3() throws ParserConfigurationException, IOException, SAXException, TransformerException {
        //得到DOM解析器的工厂实例
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        //从DOM工厂获得DOM解析器
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse("src/main/java/Jd/jd01.xml");
        //得到所有student节点信息
        NodeList studentList = doc.getElementsByTagName("student");
        NodeList scoreList = doc.getElementsByTagName("score");
        for (int i=0;i<studentList.getLength();i++){
            Element stuElement=(Element) studentList.item(i);
            String idName=stuElement.getAttribute("id");

            Element scoreElement=(Element) scoreList.item(i);
            if (idName.equals("2")){
                scoreElement.setAttribute("name","60");
            }
        }
        //保存XML文件
        TransformerFactory transformerFactory=TransformerFactory.newInstance();
        Transformer transformer=transformerFactory.newTransformer();
        DOMSource domSource=new DOMSource(doc);
        //设置编码类型
        transformer.setOutputProperty(OutputKeys.ENCODING,"gb2312");
        StreamResult result=new StreamResult(new FileOutputStream("src/main/java/Jd/jd01.xml"));
        //把DOM树转换为XML文件
        transformer.transform(domSource,result);
    }
    public static void ShowInfo4() throws ParserConfigurationException, IOException, SAXException, TransformerException {
        //得到DOM解析器的工厂实例
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        //从DOM工厂获得DOM解析器
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse("src/main/java/Jd/jd01.xml");

        //创建student节点
        Element stuElement= doc.createElement("student");
        stuElement.setAttribute("id","3");

        //创建name节点
        Element nameElement= doc.createElement("name");
        nameElement.setAttribute("name","大朗");

        //创建course节点
        Element courseElement= doc.createElement("course");
        courseElement.setAttribute("name","WEB");

        //创建score节点
        Element scoreElement= doc.createElement("score");
        scoreElement.setAttribute("name","98");

        //添加父子关系
        stuElement.appendChild(nameElement);
        stuElement.appendChild(courseElement);
        stuElement.appendChild(scoreElement);
        Element scoresElement=(Element)doc.getElementsByTagName("scores").item(0);
        scoresElement.appendChild(stuElement);

        //保存XML文件
        TransformerFactory transformerFactory=TransformerFactory.newInstance();
        Transformer transformer=transformerFactory.newTransformer();
        DOMSource domSource=new DOMSource(doc);
        //设置编码类型
        transformer.setOutputProperty(OutputKeys.ENCODING,"gb2312");
        StreamResult result=new StreamResult(new FileOutputStream("src/main/java/Jd/jd01.xml"));
        //把DOM树转换为XML文件
        transformer.transform(domSource,result);
    }

    public static void ShowInfoJd04() {
        org.dom4j.Document doc=null;
        try{
            //加载DOM树
            SAXReader saxReader=new SAXReader();
            doc=saxReader.read(new File("src/main/java/Jd/jd01.xml"));
        }catch (Exception e){
            e.printStackTrace();
        }
        //获取XML的根节点
        org.dom4j.Element root=doc.getRootElement();
        //遍历所有的student标签
        for (Iterator itStu = root.elementIterator(); itStu.hasNext();) {
            //获取第i个student元素节点
            org.dom4j.Element student = (org.dom4j.Element) itStu.next();
            Attribute attributeStu=student.attribute("id");
            String stuName = attributeStu.getText();

            org.dom4j.Element nameEle=student.element("name");
            Attribute attributeName=nameEle.attribute("name");
            String nameValue=attributeName.getText();

            org.dom4j.Element courseEle=student.element("course");
            Attribute attributeCourse=courseEle.attribute("name");
            String courseValue=attributeCourse.getText();

            org.dom4j.Element scoreEle=student.element("score");
            Attribute attributeScore=scoreEle.attribute("name");
            String scoreValue=attributeScore.getText();

            System.out.println("学生编号：" + stuName + "\t姓名：" + nameValue + "\t学科：" + courseValue + "\t成绩：" + scoreValue);
        }
    }
}
